#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexDashDecimal(L"(\\d+\\.\\d+)(-\\d+\\.\\d+)+");

const vector<string> VecStrZhLastRange = {
	"范围", "比分",
};
const vector<string> VecStrEnLastRange = {
	"range", "score", "pages", "between", "year",
};

const vector<string> VecStrZhLastFormula = {
	"公式", "数学",
};
const vector<string> VecStrEnLastFormula = {
	"formula", "math",
};


void TextNormalizer::TnReDashDecimal(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while(regex_search(*fpInWsentence, tWsmatch, mcWregexDashDecimal))
	{
		string tInSentence = wstring2utf8string(*fpInWsentence);
		string tStrReplace = "";
		string tStrDash = wstring2utf8string(tWsmatch[0]);	//匹配到横杆的字符串
		vector<string> tItemDash = StrSplit(tStrDash, "-");	//按横杆分开
		size_t tItemTotal = tItemDash.size();

		//超过三项的情况，__-__-__，采用减法读法。
		if(tItemTotal >= 3) {
			tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_subtraction:RME_zh_subtraction);
		}
		//等于2项的情况，__-__
		else if(tItemTotal == 2) {
			{
				//提取数字前面的字符串
				const vector<wstring> tcZhVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_ZH);
				const vector<wstring> tcEnVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_EN);

				//如果存在范围类型关键词，则以范围读法
				int tCountRange = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastRange, LANG_ZH)
								 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastRange, LANG_EN);
				//如果存在公式等其它，则以减法读法
				int tCountFormula = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastFormula, LANG_ZH)
								 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastFormula, LANG_EN);

				if(tCountRange >= tCountFormula) {
					tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_range:RME_zh_range);
				}
				else {
					tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_subtraction:RME_zh_subtraction);
				}
			}
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
